The rmarkdown package is a relatively complete ecosystem for authoring documents. The tasks that you could do with R Markdown are:

Document Metadata

The body of a document follows the metadata.

Markdown Syntax

You can write your report in plain text.

  • Italic is surrounded by underscores or asterisks, e.g., _text_ or *text*.
  • Bold the text by using a pair of double asterisks (**text**)
  • To subscript the text uses a pair of tildes (~) (e.g., H~2~O renders H2O).
  • To superscript uses a pair of carets (^) (e.g., Cu^2+^ renders Cu2+).
  • Hyperlinks [text](link), e.g., [RStudio](https://www.rstudio.com)
  • Images: just add an exclamation mark, e.g., ![alt text or image title](path/to/image).
  • Footnotes: put inside the square brackets after a caret ^[], e.g., ^[This is a footnote.].

Section headers:

# First-level header

## Second-level header

### Third-level header

First-level header

Second-level header

Third-level header

Unordered list:

The items start with *, -, or +, and it can be nested one list within another list by indenting the sub-list

- one item
- one item
- one item
    - one more item
    - one more item
    - one more item
  • one item
  • one item
  • one item
    • one more item
    • one more item
    • one more item

Ordered list:

The items start with numbers (you can also nest lists within lists)

1. the first item
2. the second item
3. the third item
    - one unordered item
    - one unordered item
  1. the first item
  2. the second item
  3. the third item
    • one unordered item
    • one unordered item

R code chunks and inline R code

To insert an R code chunk you can either use the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I (Cmd + Option + I on macOS).

In a code chunk you can produce text output, tables, or graphics and you have control over all these output via chunk options, which can be provided inside the curly braces (between ```{r and }).

Plots

tail(mtcars)
##                 mpg cyl  disp  hp drat    wt qsec vs am gear carb
## Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.7  0  1    5    2
## Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.9  1  1    5    2
## Ford Pantera L 15.8   8 351.0 264 4.22 3.170 14.5  0  1    5    4
## Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.5  0  1    5    6
## Maserati Bora  15.0   8 301.0 335 3.54 3.570 14.6  0  1    5    8
## Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.6  1  1    4    2

plot <- ggplot(cars, aes(x = speed)) +
  geom_histogram(aes(y=..density..), color="black", fill = "steelblue", binwidth = 0.5, alpha = 0.2) +
  geom_density()
plot

Tables

To work with tables, we can use the libraries

install.packages("kableExtra")
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows

Let’s use the mtcars dataset available in R

dt <- mtcars[1:7, 1:6]
dt
##                    mpg cyl disp  hp drat    wt
## Mazda RX4         21.0   6  160 110 3.90 2.620
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875
## Datsun 710        22.8   4  108  93 3.85 2.320
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215
## Hornet Sportabout 18.7   8  360 175 3.15 3.440
## Valiant           18.1   6  225 105 2.76 3.460
## Duster 360        14.3   8  360 245 3.21 3.570

The basic HTML output of kable.

kable(dt)
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440
Valiant 18.1 6 225 105 2.76 3.460
Duster 360 14.3 8 360 245 3.21 3.570

The HTML table using the kable_styling() apply twitter bootstrap theme to the table.

dt %>%
  kable() %>%
  kable_styling()
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440
Valiant 18.1 6 225 105 2.76 3.460
Duster 360 14.3 8 360 245 3.21 3.570

There are some predefined classes that you can use as striped, bordered, hover, condensed, and responsive.

kable(dt) %>%
  kable_styling(bootstrap_options = c("striped", "hover"))
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440
Valiant 18.1 6 225 105 2.76 3.460
Duster 360 14.3 8 360 245 3.21 3.570

For shorter row height, you can use the option condensed.

kable(dt) %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440
Valiant 18.1 6 225 105 2.76 3.460
Duster 360 14.3 8 360 245 3.21 3.570

For more examples of how to manipulate table styles here

Interactive plots

library(plotly)
## Warning: package 'plotly' was built under R version 3.5.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
p <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
p
head(economics)
## # A tibble: 6 x 6
##   date         pce    pop psavert uempmed unemploy
##   <date>     <dbl>  <int>   <dbl>   <dbl>    <int>
## 1 1967-07-01  507. 198712    12.5     4.5     2944
## 2 1967-08-01  510. 198911    12.5     4.7     2945
## 3 1967-09-01  516. 199113    11.7     4.6     2958
## 4 1967-10-01  513. 199311    12.5     4.9     3143
## 5 1967-11-01  518. 199498    12.5     4.7     3066
## 6 1967-12-01  526. 199657    12.1     4.8     3018
plot_ly(economics, x = ~date, y = ~unemploy / pop)
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
plot_ly(economics, 
        x = ~date,
        y = ~unemploy, 
        type = 'scatter', 
        mode = 'lines') %>%
layout(title = "Unemploy") 

# Arranging plotly objects

The subplot() function provides a flexible interface for merging multiple plotly objects into a single object.

p1 <- plot_ly(economics, x = ~date, y = ~unemploy) %>% 
  add_lines(name = "unemploy")
p2 <- plot_ly(economics, x = ~date, y = ~uempmed) %>% 
  add_lines(name = "uempmed")
subplot(p1, p2)

When we are evaluating time series on diferent y scale and common x scale, we also use the subplot function.

vars <- setdiff(names(economics), "date")
plots <- lapply(vars, function(var) {
  plot_ly(economics, x = ~date, y = as.formula(paste0("~", var))) %>%
    add_lines(name = var)
})
subplot(plots, nrows = length(plots), shareX = TRUE, titleX = FALSE)
library(gapminder)
## Warning: package 'gapminder' was built under R version 3.5.3
head(gapminder)
## # A tibble: 6 x 6
##   country     continent  year lifeExp      pop gdpPercap
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.
## 2 Afghanistan Asia       1957    30.3  9240934      821.
## 3 Afghanistan Asia       1962    32.0 10267083      853.
## 4 Afghanistan Asia       1967    34.0 11537966      836.
## 5 Afghanistan Asia       1972    36.1 13079460      740.
## 6 Afghanistan Asia       1977    38.4 14880372      786.

Now, it is your turn to create a beatiful HTML document in Rmarkown. Use one of the datasets from the last meeting and please, includes:

This presentation was based on